﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;

namespace PEXAM.Admin.Page_Admin
{
    public partial class OperatorGroup : System.Web.UI.Page
    {
        string sErr = "";
        string _strPOG = "POG";
        string _db = "EXA";
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {


            }

        }






        private DTO.POGInfo GetDataFromForm()
        {
            DTO.POGInfo result = new DTO.POGInfo();
            result.ROLE_ID = result.ROLE_ID1 = txtRoleId.Text;
            result.ROLE_NAME = txtRoleName.Text;
            result.PASS_MIN_LEN = txtPassLen.Text;
            result.PASS_VALID = txtPassValid.Text;
            result.TB = _strPOG;
            result.RPT_CODE = "";
            return result;
        }

        private void SetDataToForm(DTO.POGInfo objEntr)
        {
            txtRoleId.Text = objEntr.ROLE_ID;
            txtRoleName.Text = objEntr.ROLE_NAME;
            txtPassLen.Text = objEntr.PASS_MIN_LEN;
            //txtDataAccess.Text = objEntr.Data
            txtPassValid.Text = objEntr.PASS_VALID;

        }

        private void RefreshForm()
        {
            txtRoleId.Text = "";
            txtRoleName.Text = "";
            txtPassValid.Text = "";
            txtPassLen.Text = "";
            lbErr.Text = "";
        }


        protected void dgvList_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            BUS.POGControl control = new BUS.POGControl();
            dgvList.DataSource = control.GetAll(ref sErr);
        }

        protected void dgvList_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "SELECTED")
            {
                string id = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ROLE_ID"].ToString();
                BUS.POGControl ctr = new BUS.POGControl();
                SetDataToForm(ctr.Get(id, ref sErr));
                lbErr.Text = "";
            }
            if (e.CommandName.ToUpper() == "DETAIL")
            {
                string id = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ROLE_ID"].ToString();

                Session["POGCode"] = id;
                Response.Redirect("OperatorPermission.aspx");
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            RefreshForm();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DTO.POGInfo info = GetDataFromForm();
            BUS.POGControl control = new BUS.POGControl();
            if (!control.IsExist(info.ROLE_ID))
            {
                control.Add(info, ref sErr);
                if (sErr == "")
                {
                    lbErr.Text = "Insert successfull";
                    
                }
                else
                { lbErr.Text = sErr; }
            }
            else
            {
                sErr = control.Update(info);
                if (sErr == "")
                {
                    lbErr.Text = "Update successfull";
                }
                else
                { lbErr.Text = sErr; }
            }
            dgvList.Rebind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridItem item in dgvList.SelectedItems)
            {
                if (item.ItemIndex >= 0)
                {
                    string id = item.OwnerTableView.DataKeyValues[item.ItemIndex]["ROLE_ID"].ToString();
                    BUS.POGControl ctr = new BUS.POGControl();
                    sErr = ctr.Delete(id);
                    BUS.POPControl popCtr = new BUS.POPControl();
                    sErr += popCtr.Delete(id, _db);
                }
            }
            dgvList.Rebind();
        }
    }
}
